/*
 * LevelDialog.java
 *
 * Created on Sep 23, 2011, 11:59:10 AM
 */
package com.photoexpert;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.image.RescaleOp;
import java.util.logging.Level;
import javax.swing.JDialog;
import javax.swing.JPanel;

/**
 *
 * @author PANYIXIN
 */
class LevelDialog extends JDialog {

    /** Creates new form LevelDialog */
    LevelDialog() {
        super(App.Instance(), true);
        initComponents();
        setLocation(200, 200);
        setVisible(true);
    }

    static private class LevelPanel extends JPanel {

        private LevelPanel() { }

        @Override
        public void paint(Graphics g) {
            g.setColor(Color.WHITE);
            Rectangle r = getVisibleRect();
            g.fillRect(0, 0, r.width, r.height);
            g.setColor(Color.LIGHT_GRAY);
            g.drawLine(0, 255, 255, 0);
            g.drawLine(0, 128, 255, 128);
            g.drawLine(128, 0, 128, 255);
            g.setColor(Color.BLACK);
            g.drawLine(_x1, _y1, _x2, _y2);
        }

        private void addValue(int v) {
            _x2 = _x1;
            _y2 = _y1;
            changeValue(v);
        }

        private void changeValue(int v) {
            if(v > 255) {
                _x1 = v - 255;
                _y1 = 0;
            } else if(v >= 0) {
                _x1 = 0;
                _y1 = 255 - v;
            } else if( v > -255) {
                _x1 = -v;
                _y1 = 255;
            } else {
                _x1 = 255;
                _y1 = 510 + v;
            }
            repaint();
        }

        private int     _x1 = 0;
        private int     _y1 = 255;
        private int     _x2 = 255;
        private int     _y2 = 0;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        _north = new javax.swing.JSlider();
        _west = new javax.swing.JSlider();
        _south = new javax.swing.JSlider();
        _east = new javax.swing.JSlider();
        _level = new LevelDialog.LevelPanel();
        _previewLabel = new javax.swing.JLabel();
        _previewPane = PreviewPane.CreateInstance();
        _cancel = new javax.swing.JButton();
        _ok = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Level");
        setModal(true);
        setResizable(false);

        _north.setMajorTickSpacing(32);
        _north.setMaximum(255);
        _north.setMinorTickSpacing(16);
        _north.setPaintTicks(true);
        _north.setValue(255);
        _north.setFocusable(false);
        _north.setPreferredSize(new java.awt.Dimension(256, 31));
        _north.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                _northStateChanged(evt);
            }
        });

        _west.setMajorTickSpacing(32);
        _west.setMaximum(255);
        _west.setMinorTickSpacing(16);
        _west.setOrientation(javax.swing.JSlider.VERTICAL);
        _west.setPaintTicks(true);
        _west.setValue(0);
        _west.setFocusable(false);
        _west.setPreferredSize(new java.awt.Dimension(31, 256));
        _west.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                _westStateChanged(evt);
            }
        });

        _south.setMajorTickSpacing(32);
        _south.setMaximum(255);
        _south.setMinorTickSpacing(16);
        _south.setPaintTicks(true);
        _south.setValue(0);
        _south.setFocusable(false);
        _south.setPreferredSize(new java.awt.Dimension(256, 31));
        _south.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                _southStateChanged(evt);
            }
        });

        _east.setMajorTickSpacing(32);
        _east.setMaximum(255);
        _east.setMinorTickSpacing(16);
        _east.setOrientation(javax.swing.JSlider.VERTICAL);
        _east.setPaintTicks(true);
        _east.setValue(255);
        _east.setFocusable(false);
        _east.setPreferredSize(new java.awt.Dimension(31, 256));
        _east.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                _eastStateChanged(evt);
            }
        });

        _level.setPreferredSize(new java.awt.Dimension(256, 256));

        javax.swing.GroupLayout _levelLayout = new javax.swing.GroupLayout(_level);
        _level.setLayout(_levelLayout);
        _levelLayout.setHorizontalGroup(
            _levelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 260, Short.MAX_VALUE)
        );
        _levelLayout.setVerticalGroup(
            _levelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 256, Short.MAX_VALUE)
        );

        _previewLabel.setText("Preview:");

        _previewPane.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        _previewPane.setPreferredSize(new java.awt.Dimension(252, 192));

        javax.swing.GroupLayout _previewPaneLayout = new javax.swing.GroupLayout(_previewPane);
        _previewPane.setLayout(_previewPaneLayout);
        _previewPaneLayout.setHorizontalGroup(
            _previewPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 248, Short.MAX_VALUE)
        );
        _previewPaneLayout.setVerticalGroup(
            _previewPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 190, Short.MAX_VALUE)
        );

        _cancel.setText("Cancel");
        _cancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                _cancelActionPerformed(evt);
            }
        });

        _ok.setText("OK");
        _ok.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                _okActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(_west, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(_south, javax.swing.GroupLayout.DEFAULT_SIZE, 260, Short.MAX_VALUE)
                    .addComponent(_north, javax.swing.GroupLayout.DEFAULT_SIZE, 260, Short.MAX_VALUE)
                    .addComponent(_level, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 260, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(_east, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(_cancel)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(_previewLabel)
                        .addComponent(_previewPane, javax.swing.GroupLayout.PREFERRED_SIZE, 250, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(_ok, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(23, 23, 23))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(_north, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(_previewLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(_level, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(_west, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(_east, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(_south, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(_previewPane, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 53, Short.MAX_VALUE)
                        .addComponent(_ok)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(_cancel)))
                .addContainerGap(24, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void _cancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event__cancelActionPerformed
        setVisible(false);
        dispose();
    }//GEN-LAST:event__cancelActionPerformed

    private void _okActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event__okActionPerformed
        if(_op != null)
            Implementor.Instance().adjust(_op);
        setVisible(false);
        dispose();
    }//GEN-LAST:event__okActionPerformed

    private void _northStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event__northStateChanged
        int v = _north.getValue() + 255;
        if(_v1 > 255) {
            _v1 = v;
            ((LevelPanel)_level).changeValue(_v1);
        } else {
            _v2 = _v1;
            _v1 = v;
            ((LevelPanel)_level).addValue(_v1);
        }
        preview();
    }//GEN-LAST:event__northStateChanged

    private void _westStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event__westStateChanged
        int v = _west.getValue();
        if(_v1 <= 255 && _v1 >= 0) {
            _v1 = v;
            ((LevelPanel)_level).changeValue(_v1);
        } else {
            _v2 = _v1;
            _v1 = v;
            ((LevelPanel)_level).addValue(_v1);
        }
        preview();
    }//GEN-LAST:event__westStateChanged

    private void _eastStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event__eastStateChanged
        int v = -255 - _east.getValue();
        if(_v1 <= -255) {
            _v1 = v;
            ((LevelPanel)_level).changeValue(_v1);
        } else {
            _v2 = _v1;
            _v1 = v;
            ((LevelPanel)_level).addValue(_v1);
        }
        preview();
    }//GEN-LAST:event__eastStateChanged

    private void _southStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event__southStateChanged
        int v = 0 - _south.getValue() ;
        if(_v1 >= -255 && _v1 <= 0) {
            _v1 = v;
            ((LevelPanel)_level).changeValue(_v1);
        } else {
            _v2 = _v1;
            _v1 = v;
            ((LevelPanel)_level).addValue(_v1);
        }
        preview();
    }//GEN-LAST:event__southStateChanged

    private void preview() {
        int x1, x2, y1, y2;
        if(_v1 > 255) {
            x1 = _v1 - 255;
            y1 = 255;
        } else if(_v1 >= 0) {
            x1 = 0;
            y1 = _v1;
        } else if(_v1 > -255) {
            x1 = -_v1;
            y1 = 0;
        } else {
            x1 = 255;
            y1 = -255 - _v1;
        }
        if(_v2 > 255) {
            x2 = _v2 - 255;
            y2 = 255;
        } else if(_v2 >= 0) {
            x2 = 0;
            y2 = _v2;
        } else if(_v2 > -255) {
            x2 = -_v2;
            y2 = 0;
        } else {
            x2 = 255;
            y2 = -255 - _v2;
        }
        if(x2 == x1) {
            App._logger.log(Level.WARNING, "LevelDialog:x1=x2={0}", x1);
            return;
        }
        float scale = (y2 - y1) * 1f / (x2 - x1);
        float offset = y1 - scale * x1;
        _op = new RescaleOp(scale, offset, null);
        ((PreviewPane)_previewPane).setOp(_op);
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton _cancel;
    private javax.swing.JSlider _east;
    private javax.swing.JPanel _level;
    private javax.swing.JSlider _north;
    private javax.swing.JButton _ok;
    private javax.swing.JLabel _previewLabel;
    private javax.swing.JPanel _previewPane;
    private javax.swing.JSlider _south;
    private javax.swing.JSlider _west;
    // End of variables declaration//GEN-END:variables
    // north  : 256 -> 510
    // west   : 0 -> 255
    // east   : -255 -> -510
    // sourth : 0 -> -255
    private int         _v1 = 0;
    private int         _v2 = 510;
    private RescaleOp   _op = null;
}
